package com.ruoyi.gpt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gpt.domain.bo.SignInBo;
import com.ruoyi.gpt.domain.vo.SignInVo;
import com.ruoyi.gpt.domain.SignIn;
import com.ruoyi.gpt.mapper.SignInMapper;
import com.ruoyi.gpt.service.ISignInService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * chatGpt用户每日签到Service业务层处理
 *
 * @author ztx
 * @date 2023-04-08
 */
@RequiredArgsConstructor
@Service
public class SignInServiceImpl implements ISignInService {

    private final SignInMapper baseMapper;

    /**
     * 查询chatGpt用户每日签到
     */
    @Override
    public SignInVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public SignIn querySignInByUserId(Long userId) {
        LambdaQueryWrapper<SignIn> qw = Wrappers.lambdaQuery();
        qw.eq(SignIn::getUserId, userId);
        return baseMapper.selectOne(qw);
    }

    /**
     * 查询chatGpt用户每日签到列表
     */
    @Override
    public TableDataInfo<SignInVo> queryPageList(SignInBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SignIn> lqw = buildQueryWrapper(bo);
        Page<SignInVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询chatGpt用户每日签到列表
     */
    @Override
    public List<SignInVo> queryList(SignInBo bo) {
        LambdaQueryWrapper<SignIn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SignIn> buildQueryWrapper(SignInBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SignIn> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SignIn::getUserId, bo.getUserId());
        lqw.eq(bo.getContinueDays() != null, SignIn::getContinueDays, bo.getContinueDays());
        lqw.eq(bo.getTotalDays() != null, SignIn::getTotalDays, bo.getTotalDays());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), SignIn::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getUserNickname()), SignIn::getUserNickname, bo.getUserNickname());
        return lqw;
    }

    /**
     * 新增chatGpt用户每日签到
     */
    @Override
    public Boolean insertByBo(SignInBo bo) {
        SignIn add = BeanUtil.toBean(bo, SignIn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改chatGpt用户每日签到
     */
    @Override
    public Boolean updateByBo(SignInBo bo) {
        SignIn update = BeanUtil.toBean(bo, SignIn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SignIn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除chatGpt用户每日签到
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
